package LecteurMp3;


import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Mixer;

import sheepmixView.client.model.Musique;


public class MultiPlayer extends Observable implements Observer {

	private LinkedList<Musique> pathList;
	private LinkedList<Musique> userPathList;
	private Mp3Player p1, p2;
	private boolean p1IsPlaying, p2IsPlaying;
	private Mixer mixer;
	private Musique currentPlayedMusique;

	public MultiPlayer() {
		mixer = AudioSystem.getMixer(null);
		pathList     = new LinkedList<Musique>();
		userPathList = new LinkedList<Musique>();
		p1IsPlaying  = false;
		p2IsPlaying  = false;
	}

	public Musique getPlayedMusic() {
		return currentPlayedMusique;
	}
	
	public LinkedList<Musique> getPlaylist(int nbMusiqueARetourner) {
		
		LinkedList<Musique> result = new LinkedList<Musique>();
		int i = 0;
		for(int j = 0; j < userPathList.size(); j++) {
			if (i < nbMusiqueARetourner) {
				result.add(userPathList.get(j));
				i++;
			}
			else
				break;
			
		}
		for(int j = 0; j < pathList.size(); j++) {
			if (i < nbMusiqueARetourner) {
				result.add(pathList.get(j));
				i++;
			}
			else
				break;
		}
		return result;
		
	}
	
	public void startPlaylist() {
		if(!(p1IsPlaying || p2IsPlaying))
			nextOne();
	}
	
	public void stopPlaylist() {
		if(p1IsPlaying) {
			p1.stop();
			p1IsPlaying = false;
		}
		if(p2IsPlaying) {
			p2.stop();
			p2IsPlaying = false;
		}
	}

	public void LoadPlaylist(LinkedList<Musique> pathList) {
		this.pathList = pathList;
	}
	
	public void addUserSong(Musique mp3) {
		this.userPathList.add(mp3);
	}


	public void addPlaylist(LinkedList<Musique> mp3List) {
		this.pathList.addAll(mp3List);
	}

	public void addSong(Musique mp3) {
		this.pathList.add(mp3);
	}

	public void removeSong(int index) {
		this.pathList.remove(index);
	}

	public void removeList() {
		this.pathList.clear();
	}

	public synchronized void setP1IsPlaying(boolean b) {
		p1IsPlaying = b;
	}

	public synchronized void setP2IsPlaying(boolean b) {
		p1IsPlaying = b;
	}
	
	public boolean contains(Musique m) {
		try{
			System.out.println("Contains -- : "+m);
			for (int i = 0; i < userPathList.size(); i++) {
				if(userPathList.get(i).getHTMLTitle().equals(m.getHTMLTitle())) {
					//System.out.println("user : "+ i + " - " +userPathList.size()+" -- "+pathList.get(i).getHTMLTitle());
					return true;
				}
			}
			for (int i = 0;  i < Math.min(pathList.size(), 10); i++) {
				if(pathList.get(i).getHTMLTitle().equals(m.getHTMLTitle())) {
					//System.out.println("playlist : "+ i + " - " +pathList.size()+" -- "+pathList.get(i).getHTMLTitle());
					return true;
				}
			}
			System.out.println("Ne contient pas !");
			return false;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
//	public void upVolume() {
//		if (p1.isPlaying() && !p2.isPlaying()) {
//			p1.upVolume();
//		}
//		else if (p2.isPlaying() && !p1.isPlaying()) {
//			p2.upVolume();
//		}
//	}
//	
//	public void downVolume() {
//		if (p1.isPlaying() && !p2.isPlaying()) {
//			p1.downVolume();
//		}
//		else if (p2.isPlaying() && !p1.isPlaying()) {
//			p2.downVolume();
//		}
//	}

	public synchronized boolean nextOne() {

		try {
			if(pathList.size() == 0 && userPathList.size() == 0) {
				//System.out.println("Fin de la playList");
				
			}
			else if(!p1IsPlaying && p2IsPlaying) {
				
				p1 = null;
				// On privilegie une musique ajoutee par un smartphone.
				if(userPathList.size() != 0) {
					currentPlayedMusique = userPathList.pop();
					p1 = new Mp3Player(currentPlayedMusique, mixer);
				}
				else if (pathList.size() != 0) {
					currentPlayedMusique = pathList.pop();
					p1 = new Mp3Player(currentPlayedMusique, mixer);
				}
				
				if(p1 != null) {
					p1.getTimer().addObserver(this);
					p1.play(Glb.getFadeDuration());
					p1IsPlaying = true;
				}
				p2.fadeOut(Glb.getFadeDuration());
				p2IsPlaying = false;
				
				setChanged();
				notifyObservers();
				

			}
			else if(!p2IsPlaying && p1IsPlaying) {
				p2 = null;
				
				// On privilegie une musique ajoutee par un smartphone.
				if(userPathList.size() != 0) {
					currentPlayedMusique = userPathList.pop();
					p2 = new Mp3Player(currentPlayedMusique, mixer);
				}
				else if (pathList.size() != 0) {
					currentPlayedMusique = pathList.pop();
					p2 = new Mp3Player(currentPlayedMusique, mixer);
				}
				
				if(p2 != null) {
					p2.getTimer().addObserver(this);
					p2.play(Glb.getFadeDuration());
					p2IsPlaying = true;
				}
				p1.fadeOut(Glb.getFadeDuration());
				p1IsPlaying = false;
				
				setChanged();
				notifyObservers();

			}
			else if(!p2IsPlaying && !p1IsPlaying){
				
				// On privilegie une musique ajoutee par un smartphone.
				if(userPathList.size() != 0) {
					currentPlayedMusique = userPathList.pop();
					p1 = new Mp3Player(currentPlayedMusique, mixer);
				}
				else if (pathList.size() != 0) {
					currentPlayedMusique = pathList.pop();
					p1 = new Mp3Player(currentPlayedMusique, mixer);
				}
				
				if(p1 != null) {
					p1.getTimer().addObserver(this);
					p1.play(Glb.getFadeDuration());
					p1IsPlaying = true;
				}

				setChanged();
				notifyObservers();
			}
			else {
				System.out.println("Multiplayer error : Fatal Error !!!");
				return false;
			}
		}
		catch(Exception e) {
			//e.printStackTrace();
			if (e.getMessage() != null && e.getMessage().equalsIgnoreCase("InvalidFormatException"))
					nextOne();
		}
		return true;
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		nextOne();	
	}

}
